<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-02 19:42:06
 * @LastEditTime: 2023-08-30 14:20:36
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Repositories;

use App\Models\Loom;
use App\Models\ProPot;

/**
 * @property Loom $loom
 * @property ProPot $model
 */
class ProPotRepository extends Repositories
    {
    public $relation_models = [
        "material",
        "loom",
        "creator",
        "emergency",
        "process",
        "dyecraft",
        'dyeendcraft',
        'repairreason',
    ];
    public function boot(ProPot $model, Loom $loom)
        {
        $this->model = $model;
        $this->loom  = $loom;
        }
    public function getOption($year)
        {

        $val = ($year - 3) % 12;
        switch ($val) {
            case 0:
                return 'Z';
            case 1:
                return 'S';
            case 2:
                return 'N';
            case 3:
                return 'H';
            case 4:
                return 'T';
            case 5:
                return 'L';
            case 6:
                return 'A';
            case 7:
                return 'M';
            case 8:
                return 'Y';
            case 9:
                return 'B';
            case 10:
                return 'J';
            case 11:
                return 'Q';
            }
        }
    protected function saveOqa()
        {
        return(new ProOqaRepository)->autosave(request()->toArray());
        }
    public function getDyelot($loom_id, $date = null)
        {
        $date = strtotime($date ?? date("Y-dm-d"));
        $P    = $this->getOption(date("Y", $date));
        $loom = $this->loom->whereId($loom_id)->first();
        // 
        $dyelotP  = $P . date('m', $date) . date('d', $date) . $loom->name;
        $min      = date('Y-01-01', $date) . " 00:00:00";
        $max      = date('Y-12-31', $date) . " 23:59:59";
        $dyelot   = '';
        $pot_data = ProPot::where("dyelot", 'like', "{$dyelotP}%")
            ->whereLoom_id($loom_id)
            ->where("created_at", ">=", $min)
            ->where("created_at", "<=", $max)
            ->orderBy("dyelot", "desc")
            ->first();

        if ($pot_data) {
            $num    = substr($pot_data->dyelot, strpos($pot_data->dyelot, '-') + 1);
            $dyelot = $dyelotP . "-" . ($num + 1);
            }
        else {
            $dyelot = $dyelotP . "-1";
            }

        return $dyelot;
        }

    public function getReport($ids)
        {
        $xml = $this->model->whereIn("id", $ids)->get()->map(function ($pot)
            {

            $data['rscraft_rename'] = $pot->rscode;
            $data['dyelot']        = $pot->dyelot;
            $data['customer_name'] = $pot->company_name;
            $data['color_code'] = $pot->color->code;
            $data['color_name'] = $pot->color->name;
            $data['quantity']  = $pot->yarn_quantity;
            $data['loom_name'] = $pot->loom->name;
            $data['lot_name'] = $pot->lot_name;
            //假设并缸，一个回修，就全部回修
            $pot->pro_plan->filter(function ($plan) use (&$data)
                {
                if ($plan->pro_yarn->isrepair == 1)
                    $data['reform_name'] = "回修";
                });

            $data['dgremark']      = $pot->dgremark;
            $data['customeremark'] = $pot->remark;
            $data['material_name'] = $pot->material->name;
            $data['material_spec'] = $pot->material->spec;
            $data['printer'] = auth()->user()->name;

            $data['code']   = $pot->raw_send[0]->code;
            $data['zjcost'] = $pot->raw_send[0]->zjcost;
            $data['rlcost'] = $pot->raw_send[0]->rlcost;

            $data = $pot->raw->map(function ($raw) use ($data)
                {
                $data['procedure']  = $raw->procedure;
                $data['raw_code']   = $raw->material->code;
                $data['raw_name']   = $raw->material->name;
                $data['rate']       = (float) $raw->rate;
                $data['qty']        = (float) $raw->quantity;
                $data['uunit_name'] = $raw->uunit->name;
                $data['remark'] = $raw->remark;
                return $data;
                });
            return createReport($data);
            });
        return implode("", $xml->toArray());
        }

    public function getAddRawReport($ids)
        {
        $xml = $this->model->whereIn("id", $ids)->get()->map(function ($pot)
            {

            $data['rscraft_rename'] = $pot->rscode;
            $data['dyelot']        = $pot->dyelot;
            $data['customer_name'] = $pot->company_name;
            $data['color_code'] = $pot->color->code;
            $data['color_name'] = $pot->color->name;
            $data['quantity']  = $pot->yarn_quantity;
            $data['loom_name'] = $pot->loom->name;
            $data['lot_name'] = $pot->lot_name;
            //假设并缸，一个回修，就全部回修
            $pot->pro_plan->filter(function ($plan) use (&$data)
                {
                if ($plan->pro_yarn->isrepair == 1)
                    $data['reform_name'] = "回修";
                });

            $data['dgremark']      = $pot->dgremark;
            $data['customeremark'] = $pot->remark;
            $data['material_name'] = $pot->material->name;
            $data['material_spec'] = $pot->material->spec;
            $data['printer'] = auth()->user()->name;
            $raw_send = $pot->raw_send[count($pot->raw_send) - 1];
            if ($raw_send && $raw_send->state == 2) {
                $data['code']   = $raw_send->code;
                $data['zjcost'] = $raw_send->zjcost;
                $data['rlcost'] = $raw_send->rlcost;

                $data = $raw_send->details->map(function ($raw) use ($data)
                    {
                    $data['procedure']  = $raw->procedure;
                    $data['raw_code']   = $raw->material->code;
                    $data['raw_name']   = $raw->material->name;
                    $data['rate']       = (float) $raw->rate;
                    $data['qty']        = (float) $raw->quantity;
                    $data['uunit_name'] = $raw->uunit->name;
                    $data['remark'] = $raw->remark;
                    return $data;
                    });
                }
            else {
                $data = [$data];
                }
            return createReport($data);
            });
        return implode("", $xml->toArray());
        }
    }